<?php
namespace App\Modules\Auth;

use App\Modules\AdapterApi;
use App\Modules\App\App;
use App\Services\User\UserIndex;

/**
 * Class Auth
 * @package App\Modules\Auth
 */
class Auth extends AdapterApi
{
    // 创建一条权限
    public function add(): array
    {
        $data = pick_array($this->postData,['name', 'app_name', 'server',
            'module', 'controller', 'action', 'other_check','new_auth']);
        $data['merge_auth'] = $data['server'] . '/' . $data['module'] . '/' .  $data['controller'] . '/' .  $data['action'];
        $data['new_auth'] = 'App' . '/' . 'Modules' . '/' . ucfirst(strtolower($data['module'])) . '/' .  ucfirst(strtolower($data['controller'])) . '@' .  $data['action'];
        $data['other_check'] = json_encode($data['other_check']);
        return UserIndex::Auth()->add($data);
    }
    // 编辑一条权限
    public function edit(): array
    {
        $data = pick_array($this->postData,['id','name', 'app_name', 'server',
            'module', 'controller', 'action', 'other_check']);
        return UserIndex::Auth()->edit($data);
    }
    // 给某个角色一个权限(关系表)
    public function addAuthByRole(): array
    {
        $data = pick_array($this->postData,['role_id','auth_id']);
        return UserIndex::Auth()->addAuthByRole($data);
//        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'auth_id'=>'0']);
//        return $this->oneStepRun($data, 'serveruser/auth/Auth/addAuthByRole', 'addAuthByRole', 'addAuthByRole');
    }
    // 给某个角色N个权限(关系表)
    public function addAuthsByRole(): array
    {
        $data = pick_array($this->postData,['role_id','auth_ids']);
        //$data = $this->haveIssetReceiveData(['role_id'=>' ', 'auth_ids'=>'0']);
        if ($data['auth_ids'] === '') {
            return \verify_err($data, 'auth_ids');
        }
        return UserIndex::Auth()->addAuthsByRole($data);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/addAuthsByRole', 'addAuthsByRole');
    }
    // 解除某个角色的N条权限(关系表,删除)
    public function removeAuthsByRole(): array
    {
        $data = pick_array($this->postData,['role_id','auth_ids']);

       // $data = $this->haveIssetReceiveData(['role_id'=>'0', 'auth_ids'=>' ']);
        if ($data['auth_ids'] === '') {
            return \verify_err($data, 'auth_ids');
        }
        return UserIndex::Auth()->removeAuthsByRole($data);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/removeAuthsByRole', 'removeAuthsByRole');
    }
    // 禁用一条权限
    public function stop(): array
    {
        $data = pick_array($this->postData,['id']);

        return UserIndex::Auth()->stop($data);
    }
    // 启用一条权限
    public function start(): array
    {
        $data = pick_array($this->postData,['id']);

        return UserIndex::Auth()->start($data);
    }
    /**
     * 获取权限列表分页数据
     * @return array
     */
    public function list(): array
    {
        $data = pick_array($this->postData, ['page', 'listRows' => 'list_rows', 'keywords'], false);

        return UserIndex::Auth()->list($data);
    }
   // 获取权限列表的树 结构: role->server->module->controller->action
   public function tree(): array
   {
        $data = pick_array($this->postData, []);
        return UserIndex::Auth()->tree($data);
   }
//    // 禁用某个角色的所有权限
    public function stopByRole(): array
    {
        $data = pick_array($this->postData,['role_id']);

        return UserIndex::Auth()->stopByRole($data);
    }
    // 启用某个角色的所有权限
    public function startByRole(): array
    {
        $data = pick_array($this->postData,['role_id']);

        return UserIndex::Auth()->startByRole($data);
    }
    // 获取某个角色的权限列表分页
    public function listByRole(): array
    {
        $data = pick_array($this->postData,['role_id','page','list_rows','have']);

        return UserIndex::Auth()->listByRole($data);
//        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'page'=>'1', 'list_rows'=>'']);
//        return $this->oneStepRun($data, 'serveruser/auth/Auth/listByRole', 'listByRole');
    }
    // 获取某个角色的权限所有权限列表数据
    public function treeByRole(): array
    {
        $data = pick_array($this->postData,['role_id']);

        return UserIndex::Auth()->treeByRole($data);
        $data = $this->haveIssetReceiveData(['role_id'=>'0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/treeByRole', 'treeByRole');
    }
    // 获取指定ids的权限
    public function listByIds(): array
    {
        $data = pick_array($this->postData,['ids']);

        return UserIndex::Auth()->listByIds($data);
        $data = $this->haveIssetReceiveData(['ids' => '0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/listByIds', 'listByIds');
    }
    # 在redis中操作
    // 从数据库在读取N个角色的权限,更新在redis中 给一个【auth角色集】 设置某一种或几种权限 auth-visitor,buyer:并设置时间
    // 获取一个角色集的权限,如没有则从数据库中更新一次
    public function getAuthsByRoles(): array
    {
        $data = pick_array($this->postData,['roles']);

        return UserIndex::Auth()->getAuthsByRoles($data);
        $data = $this->haveIssetReceiveData(['roles' => ' ']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/getAuthsByRoles', 'getAuthsByRoles');
    }
   //通过权限id获取 拥有该权限的角色列表和未拥有角色列表
   public function roleByAuth(): array
   {
        $data = pick_array($this->postData, ['auth_id']);
        return UserIndex::Auth()->roleByAuth($data);
   }
    //根据权限id获取权限详情
    public function authInfo(): array
    {
        $data = pick_array($this->postData, ['id']);

        return UserIndex::Auth()->authInfo($data);
    }
//    // 获取某个角色未拥有的权限列表分页
//    public function noListByRole(): array
//    {
//        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'page'=>'1', 'list_rows'=>'']);
//        return $this->oneStepRun($data, 'serveruser/auth/Auth/noListByRole', 'listByRole');
//    }
}